package sv.ues.fia.activities;


import sv.ues.fia.R;
import sv.ues.fia.bdcontrol.ControlBDHelper;
import sv.ues.fia.entity.DetalleNota;
import sv.ues.fia.session.UsuarioActual;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class NotaUpdateActivity  extends Activity{
	
	ControlBDHelper dbHelper;

	@Override
	public void onCreate(Bundle saved) {
		super.onCreate(saved);
		
		dbHelper = new ControlBDHelper(this);
		
		DetalleNota nota = (DetalleNota)UsuarioActual.getOBJECT();
		
					setContentView(R.layout.activity_detallenota_modificar);
					Button btnUpdate = (Button) findViewById(R.id.btnUpdateNota);
					final EditText edtTG = (EditText)findViewById(R.id.idTGNota);
					final EditText edtCarne = (EditText) findViewById(R.id.idCarneNota);
					final EditText edtDefensa = (EditText)findViewById(R.id.idDefensaNota);
					final EditText edtNotaObs = (EditText)findViewById(R.id.edtNotaObs);
					final EditText edtNotaDir = (EditText) findViewById(R.id.edtNotaDir);
					
					edtTG.setText(nota.getIdTG());
					edtCarne.setText(nota.getIdCarne());
					edtDefensa.setText(String.valueOf(nota.getIdDefensa()));    
					edtNotaDir.setText(String.valueOf(nota.getNotaDirector()));
					edtNotaObs.setText(String.valueOf(nota.getNotaObservador()));
					
			btnUpdate.setOnClickListener( new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					if(edtNotaDir.getText().toString().isEmpty() || edtNotaObs.getText().toString().isEmpty()){
						Toast.makeText(NotaUpdateActivity.this, NotaUpdateActivity.this.getString(R.string.emptyForm),
								Toast.LENGTH_LONG).show();
					}else{
					
					DetalleNota newNota = new DetalleNota();
					
					newNota.setIdTG(edtTG.getText().toString());
					newNota.setIdCarne(edtCarne.getText().toString());
					newNota.setIdDefensa(Integer.parseInt(edtDefensa.getText().toString()));
					newNota.setNotaDirector(Float.parseFloat(edtNotaDir.getText().toString()));
					newNota.setNotaObservador(Float.parseFloat(edtNotaObs.getText().toString()));
					
					dbHelper.abrir();
					Toast.makeText(NotaUpdateActivity.this, dbHelper.modificarNota(newNota),
							Toast.LENGTH_SHORT).show();
					dbHelper.cerrar();
					UsuarioActual.setOBJECT(null);
					NotaUpdateActivity.this.finish();
					}
					
				}
			});
					
					
					
					
	}
}